<template>
  <div class="collection-container">
    <!-- 标题部分 -->
    <div class="page-header">
      <h1 class="title">我的收藏</h1>
      <div class="header-decoration"></div>
    </div>
    
    <!-- 空状态处理 -->
    <el-empty 
      v-if="collections == null || collections.length === 0" 
      description="暂无收藏内容" 
      class="empty-state">
      <el-button type="primary" @click="$router.push('/index')">
        去浏览活动
      </el-button>
    </el-empty>

    <!-- 收藏列表 -->
    <transition-group name="fade" tag="div" class="collection-grid">
      <div class="collection-item" v-for="(collection, index) in collections" :key="collection.id">
        <div class="collection-card">
          <Collection
            :collection="collection"
            @click="navigateToActivity(collection.id)"
            class="custom-collection"
          />
          <div class="action-area">
            <el-button 
              class="remove-btn" 
              @click="showConfirmDialog(collection)"
              type="danger"
              :icon="Delete"
              size="small">
              取消收藏
            </el-button>
          </div>
        </div>
      </div>
    </transition-group>
  </div>
  
  <!-- 确认对话框 -->
  <el-dialog
    v-model="dialogVisible"
    title="确认移除"
    width="30%"
    center>
    <span>确定要取消收藏 "{{ currentCollection?.name || '此活动' }}" 吗？</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="danger" @click="confirmCancel">确认移除</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import Collection from "@/components/CollectionActivity.vue";
import { Delete } from '@element-plus/icons-vue';
import {
  getAllCollections,
  collectDeleteByThingId,
} from "@/apis/user/collection";
import { ref, reactive, onMounted } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from 'element-plus';

const router = useRouter();

// 数据
const collections = ref([]);
const dialogVisible = ref(false);
const currentCollection = ref(null);
const loading = ref(false);

// 获取数据
const getPage = async () => {
  loading.value = true;
  try {
    const result = await getAllCollections();
    if (result != undefined) {
      collections.value = result.data;
      console.log(result.data);
    }
  } catch (error) {
    console.error(error);
    ElMessage.error('获取收藏列表失败，请稍后重试');
  } finally {
    loading.value = false;
  }
};

// 显示确认对话框
const showConfirmDialog = (collection) => {
  currentCollection.value = collection;
  dialogVisible.value = true;
};

// 确认取消收藏
const confirmCancel = async () => {
  await cancel(currentCollection.value.id);
  dialogVisible.value = false;
};

// 取消收藏
const cancel = async (id) => {
  try {
    await collectDeleteByThingId(id);
    ElMessage.success('已取消收藏');
    getPage();
  } catch (error) {
    console.error(error);
    ElMessage.error('取消收藏失败，请稍后重试');
  }
};

const navigateToActivity = (activityId) => {
  router.push(`/activityDetail/${activityId}`);
};

// 页面加载时执行
onMounted(async () => {
  await getPage();
});
</script>

<style lang="less" scoped>
.collection-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.page-header {
  margin-bottom: 30px;
  text-align: center;
  position: relative;
  
  .title {
    font-size: 28px;
    color: #333;
    margin-bottom: 8px;
    font-weight: 600;
  }
  
  .header-decoration {
    height: 4px;
    width: 60px;
    background: linear-gradient(90deg, #409EFF, #67C23A);
    margin: 0 auto;
    border-radius: 2px;
  }
}

.empty-state {
  margin: 60px 0;
  
  :deep(.el-empty__image) {
    width: 120px;
    height: 120px;
  }
  
  :deep(.el-empty__description) {
    margin-top: 20px;
    color: #909399;
    font-size: 16px;
  }
  
  .el-button {
    margin-top: 20px;
  }
}

.collection-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
  margin-top: 20px;
}

.collection-item {
  border-radius: 8px;
  overflow: hidden;
}

.collection-card {
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  overflow: hidden;
  height: 100%;
  display: flex;
  flex-direction: column;
  
  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  }
}

.custom-collection {
  flex: 1;
  padding: 16px;
  cursor: pointer;
}

.action-area {
  padding: 12px 16px;
  background-color: #f9f9f9;
  border-top: 1px solid #f0f0f0;
  display: flex;
  justify-content: center;
}

.remove-btn {
  font-size: 14px;
  transition: all 0.2s ease;
  
  &:hover {
    background-color: #f56c6c;
    transform: scale(1.05);
  }
}

// 动画效果
.fade-enter-active,
.fade-leave-active {
  transition: all 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
  transform: translateY(20px);
}

// 响应式调整
@media (max-width: 768px) {
  .collection-grid {
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 16px;
  }
  
  .page-header .title {
    font-size: 24px;
  }
}

@media (max-width: 480px) {
  .collection-grid {
    grid-template-columns: 1fr;
  }
  
  .collection-container {
    padding: 16px;
  }
}
</style>